﻿using System;
using System.Linq;
using CSharpConsoleTestSolution.Models;

namespace CSharpConsoleTestSolution.Demo {
    public class P23_LinQ {

        /**
         * 1.Lambda 表达式, 时间: 35:30
         */
        public static void Lambda_expression() {
            Console.WriteLine();
            Console.WriteLine("(1.Lambda 表达式:                                     (int a, int b) => { return a + b; }");
            Console.WriteLine("完整写法:");
            Console.WriteLine("// Func<int, int, int> func = new Func<int, int, int>((int a, int b) => { return a + b; });");
            Console.WriteLine("// Func 后面直接跟 Lambda 表达式:");
            Console.WriteLine("Func<int, int, int> func = (a, b) => { return a + b; };");
            Console.WriteLine("Console.WriteLine(func.Invoke(100, 200));");
            
            // Func<int, int, int> func = new Func<int, int, int>((int a, int b) => { return a + b; });
            Func<int, int, int> func = (a, b) => { return a + b; };
            Console.WriteLine(func.Invoke(100, 200));
        }


        /**
         * 2.LinQ: .NET Language Integrated Query, 时间: 51:37
         * 依赖:
         *     LinqToSql                : 比较小的内库, 集成在.NET Framework 中, 功能不算强
         *     ADO.NET Entity Data Model: 功能比较强, 但独立于.NET Framework
         *
         *  EF(Entity Framework)
         *    1.Visual Studio 集成步骤:
         *      项目右击 -> Add Folder -> Models
         *      右击 Models -> Add -> New Items... -> 弹窗左侧选择Visual C# Items -> Data ->
         *      选择 ADO.NET Entity Data Model -> 底部Name填入已存在数据库名称: AdventureWorks2014Model -> 确定
         *      -> (Entity Data Model Wizard)弹框 -> 选择 EF Designer from database (选择数据库创建实体类) -> 确定
         *      选择数据库 -> ☑Save connection settings in App.Config as: AdventureWorks2014Entities -> Next
         *      EF版本: Entity Framework 6.x -> Next -> 选择表: Person, Product -> Finish
         *
         *    2.Rider 集成步骤:
         *      NuGet 搜索: Microsoft.EntityFrameworkCore(.Net Framework 4.6.2 下载版本3.1.32)
         */
        public static void LinQ_test() {
            Console.WriteLine();
            Console.WriteLine("2.LinQ: .NET Language Integrated Query");
            //下方的实体类, 使用 EF 自动映射数据库的表生成而来
            //TODO: 怎么自动生成实体类?
            var dbContext = new AdventureWords2014Entities();
            //查找所有人
            var allPeople = dbContext.Posts.ToList();
            //查找所有人的 FirstName
            var allFirstNames = dbContext.Posts.Select(p => p.Title).ToList();
            //查找所有姓是 Timothy 的人的 FirstName + LastName
            var allTimothy = dbContext.Posts.Where(p => p.Title == "Timothy").Select(p => $"{p.Title} {p.Content}").ToList();
            //是否所有人都是 Timothy 开头
            var allPeopleFirstNameIsTimothy = dbContext.Posts.All(p => p.Title == "Timothy");
            //是否有人的名字开头是 Timothy
            var anyPeopleFirstNameIsTimothy = dbContext.Posts.Any(p => p.Title == "Timothy");
            //根据名字分组
            var groups = dbContext.Posts.GroupBy(p => p.Title).ToList();
            foreach (var group in groups) {
                Console.WriteLine($"{group.Key}\t{group.Count()}");
            }
            //查询 FirstName = Timothy 的个数
            var count = dbContext.Posts.Count(p => p.Title == "Timothy");
        }
    }
}